package net.wofly.cashier.table.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import net.wofly.cashier.table.util.TableStatus;
import net.wofly.common.util.ResultCode;
import net.wofly.common.util.ReturnBean;
import org.hibernate.exception.DataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.wofly.cashier.table.repository.TableRepository;
import net.wofly.cashier.table.domain.Table;
import net.wofly.cashier.table.service.ITableService;

/**
 * 
 * 
 * 
 * @author liujianbo
 *
 */
@Service("iTableService")
@Transactional
public class TableServiceImpl implements ITableService {

	@Autowired
	TableRepository tableRepository;

	@Override
	public Table save(Table entity) {
		return tableRepository.save(entity);
	}

	@Override
	public void delete(Table entity) {
		tableRepository.delete(entity);
	}

	@Override
	public void delete(String key) {
		tableRepository.delete(key);
	}

	@Override
	public Table findOne(String key) {
		return tableRepository.findOne(key);
	}

	@Override
	public List<Table> save(List<Table> entityList) {
		return (List<Table>) tableRepository.save(entityList);
	}

	@Override
	public ReturnBean<Table> addTable(Table table) {
		List<Table> tables = tableRepository.findByTableIDAndStatus(table.getTableID(), 0);
		if (tables.size() > 0) {
			return new ReturnBean<Table>(ResultCode.ERROR_SERVICE, "存在ID相同的餐桌，不能添加！");
		}

		List<Table> tableList = tableRepository.findByTableNameAndStatus(table.getTableName(), 0);
		if (tableList.size() > 0) {
			return new ReturnBean<Table>(ResultCode.ERROR_SERVICE, "存在名称相同的餐桌，不能添加！");
		}

		table.setStatus(0);
		table.setModifyDate(System.currentTimeMillis());

		return new ReturnBean<Table>(ResultCode.SUCCESS, "添加餐桌成功！", tableRepository.save(table));
	}

	@Override
	public ReturnBean<String> delTable(String tableID) {
		tableRepository.updateStatusByTableID(1, tableID);
		return new ReturnBean<String>(ResultCode.SUCCESS, "删除餐桌成功！");
	}

	@Override
	public ReturnBean<List<Table>> getAllTable(Integer status) {
		List<Table> tables;
		if (status == null || status == -1) {
			tables = (ArrayList) tableRepository.findAll();
		} else {
			tables = tableRepository.findByStatus(status);
		}
		return new ReturnBean<List<Table>>(ResultCode.SUCCESS, "获取所有餐桌成功！", tables);
	}

	@Override
	public ReturnBean<Table> updateTable(Table table) {
		List<Table> tableList = tableRepository.findByTableNameAndStatus(table.getTableName(), 0);
		if (tableList.size() > 0) {
			for (Table entity : tableList) {
				if (table.getTableName().equals(entity) && !table.getTableID().equals(entity.getTableID())) {
					return new ReturnBean<Table>(ResultCode.ERROR_SERVICE, "存在名称相同的餐桌，不能添加！");
				}
			}
		}

		table.setStatus(0);
		table.setModifyDate(System.currentTimeMillis());

		return new ReturnBean<Table>(ResultCode.SUCCESS, "更新餐桌成功！", tableRepository.save(table));
	}

	@Override
	public ReturnBean<Table> updateTableStatus(String tableID, Integer tableStatus) {
		Table table = tableRepository.findOne(tableID);
		if (table == null) {
			return new ReturnBean<Table>(ResultCode.ERROR_SERVICE, "此餐桌不存在，不能修改餐桌的状态！");
		}

		if ((tableStatus != TableStatus.空闲.getValue() && (tableStatus - table.getTableStatus()) == 10)
				|| (tableStatus == TableStatus.空闲.getValue() && TableStatus.收桌.getValue() == table.getTableStatus())) {
			table.setTableStatus(tableStatus);
			table.setModifyDate(System.currentTimeMillis());
			return new ReturnBean<Table>(ResultCode.SUCCESS, "更新餐桌状态成功！", tableRepository.save(table));
		}
		return new ReturnBean<Table>(ResultCode.ERROR_SERVICE, "更新餐桌状态失败, 只能更改为下一个状态！");
	}
}
